package com.lincoln.cloud.servercenter.protocol;

import com.lincoln.cloud.common.bean.ServerMessage;
import com.lincoln.cloud.common.utils.CommondValue;
import com.lincoln.cloud.servercenter.abstracter.NettyCommunicationProtocol;
import com.lincoln.cloud.servercenter.bean.ServerCenterContext;
import com.lincoln.framework.annotation.Execute;
import com.lincoln.framework.bean.AbstractMsgNoExecute;
import com.lincoln.framework.bean.AbstractMsgNoProtocol;
import com.lincoln.framework.interfaces.BaseProtocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 获取一个服务器ip及其端口{[no]|通过名字随机获取服务|1|[app-name]|[md5]}
 *
 * @author lincoln
 */
@Execute(deviceType = 1, protocolType = 1)
public class AchieveServerByName extends AbstractMsgNoExecute {
    private static final Logger logger = LoggerFactory.getLogger(AchieveServerByName.class);

    @Override
    public byte[] getServerCmd() {
        return CommondValue.ACHIEVE_SERVER_CMD.getBytes();
    }


    @Override
    public byte[] getClientCmd() {
        return CommondValue.ACHIEVE_CLIENT_CMD.getBytes();
    }

    @Override
    public Logger getLogger() {
        return logger;
    }


    @Override
    public boolean decoder(BaseProtocol protocol) {
        return true;
    }

    @Override
    public boolean execute(BaseProtocol protocol) {
        ServerMessage serverMessage = NettyCommunicationProtocol.protocol2serverMessage((AbstractMsgNoProtocol) protocol);
        String appName = new String(serverMessage.getMsg());
        return ServerCenterContext.communicationProtocol().achieveServer(appName);
    }
}
